package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.core.domain.model.ReqStringParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallZone;
import com.tencreat.domain.mall.MallZoneGoods;
import com.tencreat.service.mall.IMallZoneGoodsService;
import com.tencreat.service.mall.IMallZoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 专区Controller
 *
 * @author wangkai
 * @date 2021-11-22
 */
@Api(tags = "专区api")
//@RestController
@RequestMapping("/mall/zone")
public class MallZoneController extends BaseController {
    //@Autowired
    private IMallZoneService mallZoneService;

    //@Autowired
    private IMallZoneGoodsService zoneGoodsService;

    /**
     * 查询专区列表
     */
    @ApiOperation(value = "查询专区列表")
    @PreAuthorize("@ss.hasPermi('system:zone:list')")
    @PostMapping("/list")
    public HttpResult<List<MallZone>> list(@RequestBody ReqStringParam zoneName) {
        LambdaQueryChainWrapper<MallZone> wrapper = mallZoneService.lambdaQuery();
        if (StringUtils.isNotBlank(zoneName.getCommon())) {
            wrapper.like(MallZone::getZoneName, zoneName.getCommon());
        }
        return HttpResult.ok(wrapper.orderByAsc(MallZone::getSort).list());
    }

    /**
     * 导出专区列表
     */
    @ApiOperation(value = "导出专区列表")
    @PreAuthorize("@ss.hasPermi('system:zone:export')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallZone mallZone) {
        List<MallZone> list = mallZoneService.selectMallZoneList(mallZone);
        ExcelUtil<MallZone> util = new ExcelUtil<MallZone>(MallZone.class);
        return util.exportExcel(list, "zone");
    }

    /**
     * 获取专区详细信息
     */
    @ApiOperation(value = "获取专区详细信息")
    @PreAuthorize("@ss.hasPermi('system:zone:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallZone> getInfo(@RequestBody ReqIdParam id) {
        return HttpResult.ok(mallZoneService.selectMallZoneById(id));
    }

    /**
     * 新增专区
     */
    @ApiOperation(value = "新增专区")
    @PreAuthorize("@ss.hasPermi('system:zone:add')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody MallZone mallZone) {
        return to(mallZoneService.insertMallZone(mallZone));
    }

    /**
     * 修改专区
     */
    @ApiOperation(value = "修改专区")
    @PreAuthorize("@ss.hasPermi('system:zone:edit')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallZone mallZone) {
        return to(mallZoneService.updateMallZone(mallZone));
    }

    /**
     * 删除专区
     */
    @ApiOperation(value = "删除专区")
    @PreAuthorize("@ss.hasPermi('system:zone:remove')")
    @Log(title = "商品管理>商品专区", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        int count = zoneGoodsService.count(new LambdaQueryWrapper<MallZoneGoods>().eq(MallZoneGoods::getZoneId, ids.getId().get(0)));
        if (count > 0) {
            return HttpResult.error("专区下含有商品禁止删除！");
        }
        return to(mallZoneService.deleteMallZoneByIds(ids));
    }
}
